<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends Controller {

    public function index(){
        if($_GET){
            $search = I("get.title");
            $args['title'] = $search;
            $this -> assign('title',$search);
            $map['title'] = array("like","%{$search}%");
            $count = M("article") -> where($map) -> count();
            $page = new \Think\Page($count,10,$args);
        }else{
            $count = M("article") -> count();
            $page = new \Think\Page($count,10,'');
        }
        $page -> setConfig('prev','上一页');
        $page -> setConfig('next','下一页');
        $show = $page -> show();
        $articles = M("article") ->where($map) -> order("id desc") ->limit($page -> firstRow.','.$page -> listRows) -> select();
//        p($articles);
        $this -> assign('count',$count);
        $this -> assign('page',$show);
        $this -> assign('articles',$articles);
        $this->display();
    }

    public function article_add(){
    	$this->display('article_add');
    }
//将文章上传到服务器
    public function AddArticle(){
    	$info['title'] = I("post.title");
    	$info['author'] = I("post.author");
    	$info['content'] = $_POST['content'];
    	$info['createTime'] = time();
    	if(M("article") -> add($info)){
    	    $this -> success('',U('index'),0);
        }else{
    	    $this -> error("添加失败");
        }
    }

    public function article_edit(){
        $id = I('id');
        $article = M('article') -> select($id);
        $this -> assign('article',$article[0]);
        $this->display('article_edit');
    }

    public function EditArticle(){
        $info['title'] = I("post.title");
        $info['author'] = I("post.author");
        $info['content'] = I("post.content");
        if(M("article") -> where("id = {$_POST['id']}") -> save($info)){
            $this -> success("修改成功",U('index'));
        }else{
            $this -> error("修改失败");
        }
    }
    //单条删除
    public function article_del(){
        $id = $_POST['id'];
        if(M("article") -> where("id = {$id}") -> delete()){
            echo "success delete";
        }
    }
    //批量删除
    public function article_delAll(){
        $ids = I("post.ids");
        $ids = substr($ids,0,strlen($ids) - 1);
        if(M("article") -> where("id in ({$ids})") -> delete()){
            echo 1111;
        }else{
            echo 2222;
        }
    }
}